// miniprogram/pages/paper_box_detail/paper_box_detail.js
var QQMapWX = require('../../qqmap-wx-jssdk');
var qqmapsdk;
var location = "";


const db=wx.cloud.database();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activity_location: "",
    judge_avater:"",
    judge_judge:"",
    judge:"",
    data:0,
    dataCopy:0,
    paper_box_src:[],
    paper_box_price:"",
    show:false,
    paper_box_num:"",
    paper_box_detail:"",
    paper_box_name:"",
    paper_box_ad:"",
    paper_box_size:[],
    paper_box_material:[],
    index:0,
    box_name:"",
    box_src:[],
    box_size:[],
    box_material:[],
    box_price:0,
    box_detail:"",
    box_num:"",
    box_xq_src:"",
    price:0,
    id:"",
    

  },

  onClickCar:function(e){
    let that=this
    db.collection('clothes_shopping_car').where({
      box_id:that.data.id
    }).get({
      success:function(res){
        if(res.data==""){
          db.collection('clothes_shopping_car').add({
            data:{
              activity_location:that.data.activity_location,
              box_src:that.data.paper_box_src[that.data.data],
              paper_box_price:that.data.paper_box_price,
              paper_box_name:that.data.paper_box_name,
              // box_area:that.data.box_area,
              // paper_box_ad:that.data.paper_box_ad,
              box_size:that.data.paper_box_size[that.data.data],
              box_material:that.data.paper_box_material[that.data.dataCopy],
              paper_box_num:1,
              box_id:that.data.id
            },success:function(res){
              console.log('商品加入购物车成功',res)
              wx.showToast({
                title: '加入成功',
              })
            },fail:function(res){
              console.log('商品加入购物车失败',res)
            }
          })
        }else{
          wx.showToast({
            title: '已有这个商品',
          })
        }
      },fail:function(res){
        console.log(res)
      }
    })

  },

  onClickBuy:function(){
    let that=this
    // console.log('that.data.integral:'+that.data.integral)
    // console.log('that.data.paper_box_price:'+that.data.paper_box_price)
    if(that.data.integral>=that.data.paper_box_price){
        db.collection('clothes_shopping_car').where({
      box_id:that.data.id
    }).get({
      success:function(res){
        console.log(res)
        if(res.data==""){
          db.collection('clothes_shopping_car').add({
            data:{
              activity_location:that.data.activity_location,
              box_src:that.data.paper_box_src[that.data.data],
              paper_box_price:that.data.paper_box_price,
              paper_box_name:that.data.paper_box_name,
              // box_area:that.data.box_area,
              // paper_box_ad:that.data.paper_box_ad,
              box_size:that.data.paper_box_size[that.data.data],
              box_material:that.data.paper_box_material[that.data.dataCopy],
              paper_box_num:1,
              box_id:that.data.id
            },success:function(res){
              console.log('商品加入购物车成功',res)
              wx.navigateTo({
                url: '../buy_clothes/buy_clothes',
              })
            },fail:function(res){
              console.log('商品加入购物车失败',res)
            }
          })
        }else{
          wx.navigateTo({
            url: '../buy_clothes/buy_clothes',
          })
        }
      },fail:function(res){
        console.log(res)
      }
    })
    }else{
      wx.showToast({
        image:"../../images/title.png",
        title: '亲您的积分不足哦',
      })
    }


  },

// 去clothes_shopping_car
  onClickShopping:function(e){
    wx.navigateTo({
      url: '../buy_clothes/buy_clothes',
    })

  },

  //  计算积分

  get_money_sum(){
    var integral=0
    let that=this
    let integral_sum=0
    for(var x=0;x<that.data.Recycle.length;x++){
      integral_sum=integral_sum+that.data.Recycle[x].integral
    }
    that.setData({
      integral:integral_sum
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    db.collection('Recycle').get().then(res=>{
      that.setData({
        Recycle: res.data
      })
      that.get_money_sum()
      console.log('Recycle获取成功')
    })


    db.collection('order').where({
      judge_state:that.data.judge_state
    }).get({
      success:function(res){
        console.log("我是最强大的")
        db.collection('order').where({
          box_name:that.data.paper_box_name
        }).get({
          success:function(res){
            console.log("我也是最强大的")
            console.log("强大下面数据："+res)
            that.setData({
              order:res.data
            })
          }
        })
      }
    })
     /*判断是第一次加载还是从position页面返回
        如果从position页面返回，会传递用户选择的地点*/
        console.log(options)
        if (options.address != null && options.address != '') {
          //设置变量 address 的值
          that.setData({
            address: options.address
          });
        } else {
          // 实例化API核心类
          qqmapsdk = new QQMapWX({
            //此key需要用户自己申请
            key: 'F22BZ-X25RK-TZ6JD-A5IAT-HQT7K-QTFSU'
          });
          
          // 调用接口
          qqmapsdk.reverseGeocoder({
            success: function (res) {
              // console.log(res.result.address);
              that.setData({
                // address: res.result.address,
                activity_location: res.result.address
              });
              // console.log(that.data.address);
            },
            fail: function (res) {
              //console.log(res);
            },
            complete: function (res) {
              //console.log(res);
            }
          });
        }

// 纸质包装盒部分
      db.collection('seckill').doc(options.id).get({
       success:function(res){
         that.setData({
           paper_box_src:res.data.src,
           paper_box_price:res.data.integrate,
           paper_box_name:res.data.name,
           box_area:res.data.area,
           paper_box_ad:res.data.ad,
           paper_box_size:res.data.size,
           paper_box_material:res.data.material,
           id:res.data._id
         })
       }
    })

// 塑料包装盒部分
      db.collection('exchangeNV').doc(options.id).get({
        success:function(res){
          that.setData({
            paper_box_src:res.data.src,
            paper_box_price:res.data.integrate,
            paper_box_name:res.data.name,
            box_area:res.data.area,
            paper_box_ad:res.data.ad,
            paper_box_size:res.data.size,
            paper_box_material:res.data.material
          })
        }
      })

//  金属包装盒部分
      db.collection('exchangeNan').doc(options.id).get({
        success:function(res){
          that.setData({
            paper_box_src:res.data.src,
            paper_box_price:res.data.integrate,
            paper_box_name:res.data.name,
            box_area:res.data.area,
            paper_box_ad:res.data.ad,
            paper_box_size:res.data.size,
            paper_box_material:res.data.material
          })
        }
      })

//  金属包装盒部分
      db.collection('snacks').doc(options.id).get({
        success:function(res){
          that.setData({
            paper_box_src:res.data.src,
            paper_box_price:res.data.integrate,
            paper_box_name:res.data.name,
            box_area:res.data.area,
            paper_box_ad:res.data.ad,
            paper_box_size:res.data.size,
            paper_box_material:res.data.material
          })
        }
      })
      // 评价部分

      db.collection('judge').get().then(res=>{ 
        that.setData({
          judge:res.data
        })
        console.log(res.data);
      })

     

    
  },

  onSure:function(e){
    let that=this;
    that.setData({
      show:false
    })

  },

  switchChose:function(e){
    let id=e.target.dataset.id;
    let that=this;
    that.setData({
      data:id
    })

  },
// 原来的程序
  switchChoseCopy:function(e){
    let id=e.target.dataset.id;
    this.setData({
      dataCopy:id
    })
  },

  getLocation: function () {
  
    wx.navigateTo({
      url: '../position/position',
    });
  },


  
  onclick:function(e){
    this.setData({
      show:true
    })

  },
  onClose:function(e){
    this.setData({
      show:false
    })
  },
 

  bindBoxChange:function(e){
    //  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
 

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    db.collection('order').where({
      judge_state:that.data.judge_state
    }).get({
      success:function(res){
        console.log("我是最强大的")
        db.collection('order').where({
          box_name:that.data.paper_box_name
        }).get({
          success:function(res){
            console.log("我也是最强大的")
            console.log("强大下面数据："+res)
            that.setData({
              order:res.data
            })
          }
        })
      }
    })
    console.log(getApp().data.activity_location);//从position跳转过来，可以
    // console.log(this.data.address);
    location = getApp().data.activity_location;
    if (location != "") {
      that.setData({
        activity_location: location
        // activity_location: this.data.address//这个没用，可能onshow最先获取不到onLoad值
      });
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})